//
//  UIBarButtonItem+Extension.swift
//  Weibo
//
//  Created by HH on 16/12/1.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit

extension UIBarButtonItem{
    
    
   // 给函数的参数提供默认值, 如果这个参数没有给它传值那么使用默认值nil, 如果给它传值那么使用的是传入过来的值
    convenience init!(title:String,imageName:String? = nil,backgroundImageName:String? = nil,target:Any?,selector:Selector,color:UIColor? = UIColor.orange) {
      
        self.init()
        
        let btn = UIButton()
        
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(color, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        
        if imageName != nil {
             btn.setImage(UIImage(named:imageName!), for: .normal)
        }
        
        if backgroundImageName != nil {
            
            btn.setBackgroundImage(UIImage(named:backgroundImageName!), for: .normal)
        }
        
        btn.addTarget(target, action: selector, for: .touchUpInside)
        
        btn.sizeToFit()
        
        self.customView = btn
        
    }
    
}
